﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CE.User;
using CE.Global;
using CE.CarInsurance;

public partial class car_insurance_ViewCompletedCarInsuranceDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.Customer) && Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.Provider))
            {
                Response.Redirect("login.aspx");
            }
        }
        else
        {
            Response.Redirect("login.aspx");
        }

        if (Page.Request.Params["ResultId"] != null)
        {
            Int64 _tempResultId = 0;
            Int64 _carId = 0;
            if (Int64.TryParse(Page.Request.Params["ResultId"], out _tempResultId))
            {
                _carId = CheckCompleteStatus(_tempResultId);
                if (_carId > 0)
                {
                    ShowDetails(_carId);
                }
                else
                {
                    Response.Redirect("~/user/viewresulthistory.aspx");
                }
            }
            else
            {
                Response.Redirect("~/user/viewresulthistory.aspx");
            }
        }
        else
        {
            Response.Redirect("~/user/viewresulthistory.aspx");
        }
    }

    private Int64 CheckCompleteStatus(Int64 _resultId)
    {
        UserResultHistory _result = new UserResultHistory();
        UserResultHistoryDB _resultdb = new UserResultHistoryDB();
        Int64 _carId = 0;
        _result = _resultdb.GetResultHistory(Convert.ToInt64(Session[General.UserId].ToString()), _resultId);
        if (_result != null && _result.IsComplete)
        {
            _carId = _result.ID1;
        }

        return _carId;
    }

    private void ShowDetails(Int64 _carId)
    {
        string _refNo = General.GetRefNumber(_carId);
        CarDetail _cardetail = new CarDetail();
        CarDetailDB _cardetaildb = new CarDetailDB();
        CE.Database.MyDatabase _db = new CE.Database.MyDatabase();

        Int64 _refCEUserId = Convert.ToInt64(Convert.ToInt64(Session[General.UserId].ToString()));
        _cardetail = _cardetaildb.GetCar(_carId, _refCEUserId);
        if (_cardetail != null)
        {
            lblGender.Text = _cardetail.Info11;
            lblDOB.Text = _cardetail.Info12;

            System.Data.DataTable _dtOccupation = new System.Data.DataTable("OccupationInfo");
            _dtOccupation = _db.GetDataTable("Select Description from tblOccupationInfo Where OccupationId = " + _cardetail.Info13);
            if (_dtOccupation != null && _dtOccupation.Rows.Count > 0)
            {
                lblOccupation.Text = _dtOccupation.Rows[0]["Description"].ToString();
            }
            _dtOccupation = null;

            lblJobNature.Text = _cardetail.Info14;

            System.Data.DataTable _dtMaritalStatus = new System.Data.DataTable("MaritalStatusInfo");
            _dtMaritalStatus = _db.GetDataTable("Select Description from tblMaritalStatusInfo Where MaritalStatusId = " + _cardetail.Info15);
            if (_dtMaritalStatus != null && _dtMaritalStatus.Rows.Count > 0)
            {
                lblMaritalStatus.Text = _dtMaritalStatus.Rows[0]["Description"].ToString();
            }
            _dtMaritalStatus = null;

            lblDrivingYearSg.Text = _cardetail.Info16;

            lblManufactYear.Text = _cardetail.ManufactYear.ToString("0000");
            lblRegYear.Text = _cardetail.RegYear.ToString("0000");
            lblOffpeak.Text = _cardetail.IsPeak ? "Yes" : "No";
            lblCarMakeModel.Text = _cardetail.MakeModelDesc;
        }

        CarPolicy _carpolicy = new CarPolicy();
        CarPolicyDB _carpolicydb = new CarPolicyDB();

        _carpolicy = _carpolicydb.GetCarPolicy(_carId, _refCEUserId);
        if (_carpolicy != null)
        {
            //lblCurrentNCD.Text = _carpolicy.NCDPercent.ToString();
            lblAnyClaim.Text = _carpolicy.IsClaimLastThreeYear ? "Yes" : "No";
            lblApplicableNCD.Text = _carpolicy.Info1 + "%";
            lblOFD.Text = _carpolicy.IsOffDriveCom ? "Yes" : "No";
            lblAddDriver.Text = _carpolicy.IsAddDriver ? "Yes" : "No";
            lblCoverCommenceDate.Text = _carpolicy.CoverageCommenceDate.ToString("dd-MMM-yyyy");
        }

        Int64 _companyId = 0;
        
        System.Data.DataTable _dtSeletectItem = new System.Data.DataTable("SelectedResultItem");
        _dtSeletectItem = _db.GetDataTable("Select * from tblCarInsuranceSelectItem WHERE RefCEUserId = " + _refCEUserId.ToString() + " And CECarId = " + _carId.ToString());
        if (_dtSeletectItem != null && _dtSeletectItem.Rows.Count > 0)
        {
            _companyId = Convert.ToInt64(_dtSeletectItem.Rows[0]["ProviderCompanyId"].ToString());
            lblAnnualPremium.Text = "$ " + Convert.ToSingle(_dtSeletectItem.Rows[0]["AnnualPremium"].ToString()).ToString("#.00");
            lblExcess.Text = "$ " + Convert.ToSingle(_dtSeletectItem.Rows[0]["Excess"].ToString()).ToString("#");
            lblPlanName.Text = _dtSeletectItem.Rows[0]["PlanName"].ToString();
        }

        if (_companyId == 29) 
        {
            LibertyCarMoreDetail _carmoredtl = new LibertyCarMoreDetail();
            LibertyCarMoreDetailDB _carmoredb = new LibertyCarMoreDetailDB();
            _carmoredtl = _carmoredb.GetCarMoreDetail(_carId, _refCEUserId);
            lblInsurer.Text = "Liberty Insurance";

            lblQuoteTitle.Text = "Quote Ref No : L" + _refNo + " - Car Insurance " + lblCarMakeModel.Text;
            if (_carmoredtl != null)
            {
                lblName.Text = _carmoredtl.Name;
                lblAddress.Text = "#" + _carmoredtl.UnitNo.ToString() + "-" + _carmoredtl.HomeNo + "<br />" + _carmoredtl.Address;
                lblContactNo.Text = _carmoredtl.MobileNo;
                lblNRICNo.Text = _carmoredtl.NRICNo;
                lblNationality.Text = _carmoredtl.Nationality;
                lblVehicleRegNo.Text = _carmoredtl.VechicleNo;
                lblEngineNo.Text = _carmoredtl.EngineNo;
                lblChassisNo.Text = _carmoredtl.ChassisNo;
            }
        }
        else if (_companyId == 30)
        {
            EtiqaCarMoreDetail _carmoredtl = new EtiqaCarMoreDetail();
            EtiqaCarMoreDetailDB _carmoredb = new EtiqaCarMoreDetailDB();
            _carmoredtl = _carmoredb.GetCarMoreDetail(_carId, _refCEUserId);
            lblInsurer.Text = "Etiqa Insurance";
            lblQuoteTitle.Text = "Quote Ref No : E" + _refNo + " - Car Insurance " + lblCarMakeModel.Text;
            if (_carmoredtl != null)
            {
                lblName.Text = _carmoredtl.Name;
                lblAddress.Text = "#" + _carmoredtl.UnitNo.ToString() + "-" + _carmoredtl.HomeNo + "<br />" + _carmoredtl.Address;
                lblContactNo.Text = _carmoredtl.ContactNo;
                lblNRICNo.Text = _carmoredtl.NRICNo;
                lblNationality.Text = _carmoredtl.Nationality;
                lblVehicleRegNo.Text = _carmoredtl.VechicleNo;
                lblEngineNo.Text = _carmoredtl.EngineNo;
                lblChassisNo.Text = _carmoredtl.ChassisNo;
            }
        }
        else if (_companyId == 23)
        {
            HsbcCarMoreDetail _carmoredtl = new HsbcCarMoreDetail();
            HsbcCarMoreDetailDB _carmoredb = new HsbcCarMoreDetailDB();
            _carmoredtl = _carmoredb.GetCarMoreDetail(_carId, _refCEUserId);
            lblInsurer.Text = "HSBC Insurance";
            lblQuoteTitle.Text = "Quote Ref No : H" + _refNo + " - Car Insurance " + lblCarMakeModel.Text;
            if (_carmoredtl != null)
            {
                lblName.Text = _carmoredtl.FamilyName + " " + _carmoredtl.GivenName;
                lblAddress.Text = "#" + _carmoredtl.UnitNo + "-" + _carmoredtl.HomeNo + "<br />" + _carmoredtl.Address;
                lblContactNo.Text = _carmoredtl.MobileNo;
                lblNRICNo.Text = _carmoredtl.NRICNo;
                lblNationality.Text = _carmoredtl.Nationality;
                lblVehicleRegNo.Text = _carmoredtl.VechicleNo;
                lblEngineNo.Text = _carmoredtl.EngineNo;
                lblChassisNo.Text = _carmoredtl.ChassisNo;
            }
        }
        else if (_companyId == 24)
        {
            RsaCarMoreDetail _carmoredtl = new RsaCarMoreDetail();
            RsaCarMoreDetailDB _carmoredb = new RsaCarMoreDetailDB();
            _carmoredtl = _carmoredb.GetCarMoreDetail(_carId, _refCEUserId);
            lblInsurer.Text = "RSA Insurance";
            lblQuoteTitle.Text = "Quote Ref No : R" + _refNo + " - Car Insurance " + lblCarMakeModel.Text;
            if (_carmoredtl != null)
            {
                lblName.Text = _carmoredtl.Name;
                lblAddress.Text = "#" + _carmoredtl.UnitNo + "-" + _carmoredtl.HomeNo + "<br />" + _carmoredtl.Address;
                lblContactNo.Text = _carmoredtl.MobileNo;
                lblNRICNo.Text = _carmoredtl.NRICNo;
                lblNationality.Text = _carmoredtl.Nationality;
                lblVehicleRegNo.Text = _carmoredtl.VechicleNo;
                lblEngineNo.Text = _carmoredtl.EngineNo;
                lblChassisNo.Text = _carmoredtl.ChassisNo;
            }
        }

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/user/viewresulthistory.aspx");
    }
}
